﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lgweb.control.inside
{
    public partial class pager : System.Web.UI.UserControl
    {
        private string styleType = "3";
        private int pageSize = 20;
        private int recordCount = 1;
        private int pageIndex = 1;
        private int totalPage = 1;
        private string linkUrl = "";
        private string isnext = "1";
        private string floatpt = "left";
        private bool _isResult = true;
        private bool _isAddPageIndex = true;

        /// <summary>
        /// 第几个分页显示样式
        /// </summary>
        public string StyleType
        {
            set { styleType = value; }
        }
        /// <summary>
        /// 是否显示下一页样式
        /// </summary>
        public string IsNext
        {
            set { isnext = value; }
        }
        /// <summary>
        /// 浮左或者浮右
        /// </summary>
        public string FloatPt
        {
            set { floatpt = value; }
        }
        /// <summary>
        /// 每页显示记录数
        /// </summary>
        public int PageSize
        {
            set { pageSize = Convert.ToInt32(value); }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount
        {
            set { recordCount = Convert.ToInt32(value); }
        }
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex
        {
            set { pageIndex = Convert.ToInt32(value); }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage
        {
            set { totalPage = Convert.ToInt32(value); }
        }

        /// <summary>
        /// 是否显示分页信息
        /// </summary>
        public bool IsResult
        {
            set { _isResult = value; }
        }

        /// <summary>
        /// 链接地址
        /// </summary>
        public string LinkUrl
        {
            set { linkUrl = value; }
        }

        /// <summary>
        /// 是否添加PageIndex参数：true-添加 false-不添加
        /// </summary>
        public bool IsAddPageIndex
        {
            set { _isAddPageIndex = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //如果结果（总记录数）为0，则将其隐藏
            if (this.recordCount < 1)
            {
                return;
            }

            
            CreatePage(this.pageIndex, this.recordCount, this.totalPage);
        }

        private void CreatePage(int pageindex, int recordCount, int totalpage)
        {
            if (linkUrl.IndexOf("{0}") < 0)
            {
                linkUrl = linkUrl.Replace("pageindex=", "PageIndex=");
                linkUrl = linkUrl.Replace("pageIndex=", "PageIndex=");

                if (linkUrl.IndexOf("PageIndex=") >= 0 || linkUrl.IndexOf("_p") >= 0)
                {
                    linkUrl = linkUrl.Replace(string.Format("PageIndex={0}", pageindex), "PageIndex={0}");
                    linkUrl = linkUrl.Replace(string.Format("_p{0}", pageindex), "_p{0}");
                }
                else
                {
                    if (linkUrl.Length > 0)
                    {
                        if (_isAddPageIndex)
                        {
                            if (linkUrl.Substring(linkUrl.Length - 1, 1) != "?")
                            {
                                if (linkUrl.Split(new string[] { "?" }, StringSplitOptions.None).Length > 1)
                                {
                                    linkUrl += "&PageIndex={0}";
                                }
                                else
                                {
                                    linkUrl += "?PageIndex={0}";
                                }
                            }
                            else
                            {
                                linkUrl += "PageIndex={0}";
                            }
                        }
                    }
                }
            }

            this.spantotal.InnerHtml = recordCount.ToString();

            int itotalPage = totalpage;

            //itotalPage = recordCount / this.pageSize;

            //if(recordCount % this.pageSize!=0)
            //{
            //    itotalPage = itotalPage + 1;
            //}


            string resultLink = this.linkUrl;

            int lastpage = pageindex + 2;
            int nextpage = pageindex + 1;
            int prepage = (pageindex <= 1 ? 1 : pageindex - 1);

            string iHtml = "";

            //if (itotalPage == 1 || pageindex == itotalPage || itotalPage == 0)
            //{
            //    this.picnext.InnerHtml = "<img src=\"/images/base/npage_next.gif\" alt=\"下一页\" class=\"g_cp s_inxt\" />";
            //}
            //else
            //{
            //    this.picnext.InnerHtml = "<a href=\"" + string.Format(resultLink, nextpage) + "\"><img src=\"/images/base/page_next.gif\" alt=\"下一页\" class=\"g_cp s_inxt\" /></a>";
            //}
            if (itotalPage == 1 || itotalPage == 0)
            {
                iHtml = iHtml + "上一页";
                iHtml = iHtml + "<span class=\"s_curr g_vm\">1</span>";
                iHtml = iHtml + "下一页";
            }
            else if (itotalPage >= 1 && itotalPage <= 5)
            {
                if (pageindex == 1)
                {
                    iHtml = iHtml + "上一页";
                }
                else
                {
                    iHtml = iHtml + "<a href=\"" + string.Format(resultLink, prepage) + "\" class=\"c_pageno\">上一页</a>";
                }
                for (int i = 1; i <= itotalPage; i++)
                {
                    if (pageindex == i)
                    {
                        iHtml = iHtml + "<span class=\"s_curr g_vm\">" + i + "</span>";
                    }
                    else
                    {
                        iHtml = iHtml + "<a class=\"g_c25 g_vm\" href=\"" + string.Format(resultLink, i) + "\">" + i + "</a>";
                    }

                }
                if (pageindex == itotalPage)
                {
                    iHtml = iHtml + "下一页";
                }
                else
                {
                    iHtml = iHtml + "<a href=\"" + string.Format(resultLink, nextpage) + "\" class=\"c_pageno\">下一页</a>";
                }
            }
            else
            {
                if (pageindex == 1)
                {
                    iHtml = iHtml + "上一页";
                }
                else
                {
                    iHtml = iHtml + "<a href=\"" + string.Format(resultLink, prepage) + "\" class=\"c_pageno\">上一页</a>";
                }

                if (pageindex > 2)
                {
                    if (pageindex == 1)
                    {
                        iHtml = iHtml + "<span class=\"s_curr g_vm\">1</span>";
                    }
                    else
                    {
                        iHtml = iHtml + "<a href=\"" + string.Format(resultLink, 1) + "\" class=\"g_c25 g_vm\">1</a>";
                    }

                    iHtml = iHtml + "<label>…</label>";
                }
                else
                {
                    if (pageindex == 1)
                    {
                        iHtml = iHtml + "<span class=\"s_curr g_vm\">1</span>";
                    }
                    else
                    {
                        iHtml = iHtml + "<a href=\"" + string.Format(resultLink, 1) + "\" class=\"g_c25 g_vm\">1</a>";
                    }
                }

                if (pageindex == 2)
                {
                    for (int i = pageindex; i <= pageindex + 3; i++)
                    {
                        if (pageindex == i)
                        {
                            iHtml = iHtml + "<span class=\"s_curr g_vm\">" + i + "</span>";
                        }
                        else
                        {
                            iHtml = iHtml + "<a class=\"g_c25 g_vm\" href=\"" + string.Format(resultLink, i) + "\">" + i + "</a>";
                        }
                    }
                }
                else if (pageindex == 1)
                {
                    for (int i = pageindex + 1; i <= pageindex + 4; i++)
                    {
                        if (pageindex == i)
                        {
                            iHtml = iHtml + "<span class=\"s_curr g_vm\">" + i + "</span>";
                        }
                        else
                        {
                            iHtml = iHtml + "<a class=\"g_c25 g_vm\" href=\"" + string.Format(resultLink, i) + "\">" + i + "</a>";
                        }
                    }
                }
                else
                {
                    if (lastpage >= itotalPage)
                    {
                        for (int i = itotalPage - 3; i <= itotalPage; i++)
                        {
                            if (pageindex == i)
                            {
                                iHtml = iHtml + "<span class=\"s_curr g_vm\">" + i + "</span>";
                            }
                            else
                            {
                                iHtml = iHtml + "<a class=\"g_c25 g_vm\" href=\"" + string.Format(resultLink, i) + "\">" + i + "</a>";
                            }
                        }
                    }
                    else
                    {
                        for (int i = pageindex - 1; i <= pageindex + 2; i++)
                        {
                            if (pageindex == i)
                            {
                                iHtml = iHtml + "<span class=\"s_curr g_vm\">" + i + "</span>";
                            }
                            else
                            {
                                iHtml = iHtml + "<a class=\"g_c25 g_vm\" href=\"" + string.Format(resultLink, i) + "\">" + i + "</a>";
                            }
                        }
                    }
                }

                if (lastpage >= itotalPage)
                {
                    iHtml = iHtml + "下一页";
                }
                else
                {
                    iHtml = iHtml + "<a href=\"" + string.Format(resultLink, nextpage) + "\" class=\"c_pageno\">下一页</a>";
                }
            }
            this.pagehtml.InnerHtml = iHtml;

        }


    }
}